/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2022-01-14 11:34:03
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-05-05 10:26:34
 * @FilePath: \code\cwp-front\pc-collaboration\src\handles\handle-list\summary\modules\return\index.js
 * @Description: 回退
 */

import { chooseAttitude, returnSender, specifiesReturn } from './helper';
import { fireEvent, getCollEventParams } from '@/handles/common/sdk';
import globalData from '@/plugins/data';
import { useBaseInfoStore } from '@/store/modules/base-info';


//回退操作
export function returnFunction(btn) {
  const {returnDetailData} = btn;
  //handle:Return: 回退上节点、SpecifiesReturnCreate: 回退发起人、SpecifiesReturn: 回退指定人
  //type:1:流程重走、2:提交给我、3:回退人自选。如果是系统内置的回退，就直接走回退
  const [handle, type] = returnDetailData ? btn.returnDetailData.split('|') : [btn.handle];
  console.log(handle, type);

  // 先触发Event
  const businessData = useBaseInfoStore().baseInfo?.businessData || {};
  const eventParams = getCollEventParams(businessData, globalData);
  fireEvent(handle, eventParams).then(res => {
    if (!res) {
      return;
    }
    switch (handle) {
      case 'specifiesReturnCreate':
        //回退发起人
        returnSender.call(this, +type, btn);
        break;
      case 'specifiesReturn':
        //指定回退
        specifiesReturn.call(this, btn);
        break;
      default:
        //回退上节点
        chooseAttitude.call(this, btn);
    }
  });
}

export default function (
  btn = { handle: 'return', label: $.i18n('permission.operation.Return'), handleOriginal: 'Return' }
) {
  return new Promise((resolve) => {
    returnFunction.call(this, btn);
    resolve();
  });
}
